

package com.hazelcast.internal.monitor.impl;

import com.hazelcast.internal.metrics.Probe;
import com.hazelcast.internal.util.Clock;
import com.hazelcast.topic.LocalTopicStats;
import com.hazelcast.topic.impl.reliable.ReliableMessageRunner;

import java.util.concurrent.atomic.AtomicLongFieldUpdater;

import static com.hazelcast.internal.metrics.MetricDescriptorConstants.*;
import static com.hazelcast.internal.metrics.ProbeUnit.MS;
import static java.util.concurrent.atomic.AtomicLongFieldUpdater.newUpdater;

public class LocalTopicStatsImpl implements LocalTopicStats {

    private static final AtomicLongFieldUpdater<LocalTopicStatsImpl> TOTAL_PUBLISHES = newUpdater(LocalTopicStatsImpl.class, "totalPublishes");
    private static final AtomicLongFieldUpdater<LocalTopicStatsImpl> TOTAL_RECEIVED_MESSAGES = newUpdater(LocalTopicStatsImpl.class, "totalReceivedMessages");
    @Probe(name = TOPIC_METRIC_CREATION_TIME, unit = MS)
    private final long creationTime;

    // These fields are only accessed through the updaters
    @Probe(name = TOPIC_METRIC_TOTAL_PUBLISHES)
    private volatile long totalPublishes;
    @Probe(name = TOPIC_METRIC_TOTAL_RECEIVED_MESSAGES)
    private volatile long totalReceivedMessages;

    public LocalTopicStatsImpl() {
        creationTime = Clock.currentTimeMillis();
    }

    @Override
    public long getCreationTime() {
        return creationTime;
    }

    @Override
    public long getPublishOperationCount() {
        return totalPublishes;
    }

    /**
     * Increment the number of locally published messages. The count can be local
     * to the member or local to a single proxy (whereas there are many proxies
     * on one member).
     *
     * @see com.hazelcast.topic.impl.TopicService
     * @see com.hazelcast.topic.impl.reliable.ReliableTopicService
     */
    public void incrementPublishes() {
        TOTAL_PUBLISHES.incrementAndGet(this);
    }

    @Override
    public long getReceiveOperationCount() {
        return totalReceivedMessages;
    }

    /**
     * Increment the number of locally received messages. The count can be local
     * to the member or local to a single listener (whereas there are many listeners
     * on one member).
     *
     * @see com.hazelcast.topic.impl.TopicService
     * @see ReliableMessageRunner
     */
    public void incrementReceives() {
        TOTAL_RECEIVED_MESSAGES.incrementAndGet(this);
    }

    @Override
    public String toString() {
        return "LocalTopicStatsImpl{" + "creationTime=" + creationTime + ", totalPublishes=" + totalPublishes + ", totalReceivedMessages=" + totalReceivedMessages + '}';
    }
}
